package com.dingwei.superiordiary.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.MyCenterBean;
import com.dingwei.superiordiary.bean.NewNoticeMessageBean;
import com.dingwei.superiordiary.mvp.presenter.MinePresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.ui.activity.AboutUsActivity;
import com.dingwei.superiordiary.ui.activity.FXYHLActivity;
import com.dingwei.superiordiary.ui.activity.FeedbackActivity;
import com.dingwei.superiordiary.ui.activity.FindUserActivity;
import com.dingwei.superiordiary.ui.activity.LoginActivity;
import com.dingwei.superiordiary.ui.activity.LookUserCenterActivity;
import com.dingwei.superiordiary.ui.activity.MyCareMeActivity;
import com.dingwei.superiordiary.ui.activity.MyCarePersonalActivity;
import com.dingwei.superiordiary.ui.activity.MyCollectPostsActivity;
import com.dingwei.superiordiary.ui.activity.MyCommentActivity;
import com.dingwei.superiordiary.ui.activity.MyHistoryPostsActivity;
import com.dingwei.superiordiary.ui.activity.MyLevelActivity;
import com.dingwei.superiordiary.ui.activity.MyLikesActivity;
import com.dingwei.superiordiary.ui.activity.MyPostsActivity;
import com.dingwei.superiordiary.ui.activity.NoticeActivity;
import com.dingwei.superiordiary.ui.activity.PointActivity;
import com.dingwei.superiordiary.ui.activity.RelatedShopActivity;
import com.dingwei.superiordiary.ui.activity.SetActivity;
import com.dingwei.superiordiary.ui.activity.ShopCartActivity;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.LazyLoadFragment;
import com.lihao.baselibrary.utils.ScreenUtils;
import com.lihao.baselibrary.weight.TitleBar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lihao
 * @date 2018/1/4
 * 首页-我的
 */

public class MineFragment extends LazyLoadFragment implements Contract.MineView {
    MinePresenter minePresenter = new MinePresenter(this);

    Intent intent = new Intent();
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.tv_user_name)
    TextView tvUserName;
    @Bind(R.id.tv_level)
    TextView tvLevel;
    @Bind(R.id.tv_register_id)
    TextView tvRegisterId;
    @Bind(R.id.tv_look_homepage)
    TextView tvLookHomepage;
    @Bind(R.id.login_layout)
    LinearLayout loginLayout;
    @Bind(R.id.not_login_layout)
    LinearLayout notLoginLayout;
    @Bind(R.id.tv_post)
    TextView tvPost;
    @Bind(R.id.post_layout)
    LinearLayout postLayout;
    @Bind(R.id.tv_care_person)
    TextView tvCarePerson;
    @Bind(R.id.care_person_layout)
    LinearLayout carePersonLayout;
    @Bind(R.id.tv_care_me)
    TextView tvCareMe;
    @Bind(R.id.care_me_layout)
    LinearLayout careMeLayout;
    @Bind(R.id.tv_praise)
    TextView tvPraise;
    @Bind(R.id.praise_layout)
    LinearLayout praiseLayout;
    @Bind(R.id.my_collect)
    SuperTextView myCollect;
    @Bind(R.id.browse_history)
    SuperTextView browseHistory;
    @Bind(R.id.my_posts)
    SuperTextView myPosts;
    @Bind(R.id.my_evaluation)
    SuperTextView myEvaluation;
    @Bind(R.id.my_praise)
    SuperTextView myPraise;
    @Bind(R.id.my_level)
    SuperTextView myLevel;
    @Bind(R.id.shopping_cart)
    SuperTextView shoppingCart;
    @Bind(R.id.news_notice)
    SuperTextView newsNotice;
    @Bind(R.id.shop)
    SuperTextView shop;
    @Bind(R.id.my_setting)
    SuperTextView mySetting;
    @Bind(R.id.share)
    SuperTextView share;
    @Bind(R.id.about_us)
    SuperTextView aboutUs;
    @Bind(R.id.feedback)
    SuperTextView feedback;
    @Bind(R.id.fake_status_bar)
    View fakeStatusBar;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.view_care_me)
    View viewCareMe;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.view_zan)
    View viewZan;
    @Bind(R.id.view_notice)
    View viewNotice;
    @Bind(R.id.tv_sign)
    TextView tvSign;

    /**
     * 设置状态栏高度 主要是设配刘海屏手机
     */
    private void setStatusBarHeight() {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtils.getStatusHeight(BaseActivity.context));
        fakeStatusBar.setLayoutParams(lp);
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_mine_new;
    }

    @Override
    protected void setBindView() {
        ButterKnife.bind(this, view);
        initView();
    }

    private void initView() {
        setStatusBarHeight();
        titleBar.setOnBackListener(new TitleBar.OnBackListener() {
            @Override
            public void onBackClick() {
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, FindUserActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        titleBar.setOnMenuListener(new TitleBar.OnMenuListener() {
            @Override
            public void onMenuClick() {
                if (BaseActivity.value.getIsLogin()) {
                    intent = new Intent(BaseActivity.context, PointActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }

            }
        });
    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (BaseActivity.value.getIsLogin()) {
            notLoginLayout.setVisibility(View.GONE);
            loginLayout.setVisibility(View.VISIBLE);
            minePresenter.getUserData();
            minePresenter.newNoticeMessage();
        } else {
            notLoginLayout.setVisibility(View.VISIBLE);
            loginLayout.setVisibility(View.GONE);

            tvPost.setText("0");
            tvCarePerson.setText("0");
            tvCareMe.setText("0");
            tvPraise.setText("0");

            viewNotice.setVisibility(View.GONE);
            viewZan.setVisibility(View.GONE);
            viewCareMe.setVisibility(View.GONE);

        }
    }


    @Override
    public void setUserData(MyCenterBean bean) {
        tvUserName.setText(bean.data.nickname);
        Glide.with(BaseActivity.context)
                .load(bean.data.portrait)
                .apply(MyApplication.headOptions)
                .into(imgHead);
        tvLevel.setText("LV." + bean.data.level);
        tvRegisterId.setText("注册ID：" + bean.data.id);

        tvPost.setText(bean.data.statistics.activityNumber + "");
        tvCarePerson.setText(bean.data.statistics.myAttentionNumber + "");
        tvCareMe.setText(bean.data.statistics.AttentionMeNumber + "");
        tvPraise.setText(bean.data.statistics.activityCommentLikeNumber + "");

        if (bean.data.statistics.collectionTotal > 0) {
            myCollect.setRightString(bean.data.statistics.collectionTotal + "");
        } else {
            myCollect.setRightString("");
        }
        if (bean.data.statistics.activityNumber > 0) {
            myPosts.setRightString(bean.data.statistics.activityNumber + "");
        } else {
            myPosts.setRightString("");
        }
        if (bean.data.statistics.commentTotal > 0) {
            myEvaluation.setRightString(bean.data.statistics.commentTotal + "");
        } else {
            myEvaluation.setRightString("");
        }
//        if (bean.data.statistics.activityCommentLikeNumber > 0) {
//            myPraise.setRightString(bean.data.statistics.activityCommentLikeNumber + "");
//        } else {
//            myPraise.setRightString("");
//        }
        if (bean.data.statistics.cartTotal > 0) {
            shoppingCart.setRightString(bean.data.statistics.cartTotal + "");
        } else {
            shoppingCart.setRightString("");
        }

        if (bean.data.statistics.noticeTotal > 0) {
            newsNotice.setRightString(bean.data.statistics.noticeTotal + "");
        } else {
            newsNotice.setRightString("");
        }


        if (bean.data.is_signin == 1) {
            tvSign.setText("已签到");
            tvSign.setBackgroundResource(R.drawable.type_bg1);
        } else {
            tvSign.setText(" 签到 ");
            tvSign.setBackgroundResource(R.drawable.type_bg);
        }

    }

    @Override
    public void signSuccess() {
        tvSign.setText("已签到");
        tvSign.setBackgroundResource(R.drawable.type_bg1);
    }

    @Override
    public void setNewNoticeMessageBean(NewNoticeMessageBean bean) {
        if (bean.data.follow_new > 0) {
            viewCareMe.setVisibility(View.VISIBLE);
        } else {
            viewCareMe.setVisibility(View.GONE);
        }
        if (bean.data.praise_new > 0) {
            viewZan.setVisibility(View.VISIBLE);
        } else {
            viewZan.setVisibility(View.GONE);
        }
        if (bean.data.notice_new > 0) {
            viewNotice.setVisibility(View.VISIBLE);
        } else {
            viewNotice.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.img_head, R.id.tv_look_homepage, R.id.tv_sign, R.id.not_login_layout, R.id.post_layout, R.id.care_person_layout,
            R.id.care_me_layout, R.id.praise_layout, R.id.my_collect, R.id.browse_history, R.id.my_posts, R.id.my_evaluation, R.id.my_praise, R.id.my_level, R.id.shopping_cart, R.id.news_notice, R.id.shop, R.id.my_setting, R.id.share, R.id.about_us, R.id.feedback})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_head:
                intent = new Intent(BaseActivity.context, LookUserCenterActivity.class);
                intent.putExtra("user_id", BaseActivity.value.getUserId());
                startActivity(intent);
                break;
            case R.id.tv_look_homepage:
                intent = new Intent(BaseActivity.context, LookUserCenterActivity.class);
                intent.putExtra("user_id", BaseActivity.value.getUserId());
                startActivity(intent);
                break;
            case R.id.tv_sign:
//                minePresenter.sign();
                intent = new Intent(BaseActivity.context, PointActivity.class);
                startActivity(intent);
                break;
            case R.id.not_login_layout:
                intent = new Intent(BaseActivity.context, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.post_layout:
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, MyPostsActivity.class);
                    intent.putExtra("userid", BaseActivity.value.getUserId());
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.care_person_layout:
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, MyCarePersonalActivity.class);
                    intent.putExtra("userid", BaseActivity.value.getUserId());
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.care_me_layout:
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, MyCareMeActivity.class);
                    intent.putExtra("userid", BaseActivity.value.getUserId());
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.praise_layout:
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, NoticeActivity.class);
                    intent.putExtra("position", 4);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.my_collect:
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, MyCollectPostsActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.browse_history:
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, MyHistoryPostsActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.my_posts:
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, MyPostsActivity.class);
                    intent.putExtra("userid", BaseActivity.value.getUserId());
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.my_evaluation:
                if (BaseActivity.value.getIsLogin()) {
                    intent = new Intent(BaseActivity.context, MyCommentActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.my_praise:
                if (BaseActivity.value.getIsLogin()) {
                    intent = new Intent(BaseActivity.context, MyLikesActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.my_level:
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, MyLevelActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.shopping_cart:
                if (BaseActivity.value.getIsLogin()) {
                    intent = new Intent(BaseActivity.context, ShopCartActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }

                break;
            case R.id.news_notice:
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, NoticeActivity.class);
                    intent.putExtra("position", 0);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.shop:
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, RelatedShopActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.my_setting:
                intent.setClass(BaseActivity.context, SetActivity.class);
                startActivity(intent);
                break;
            case R.id.share:
                intent.setClass(BaseActivity.context, FXYHLActivity.class);
                startActivity(intent);
                break;
            case R.id.about_us:
                intent.setClass(BaseActivity.context, AboutUsActivity.class);
                startActivity(intent);
                break;
            case R.id.feedback:
                if (BaseActivity.value.getIsLogin()) {
                    intent.setClass(BaseActivity.context, FeedbackActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            default:
                break;
        }
    }
}
